<?php
namespace App\Http\Controllers\Admin\UserManager;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use App\Services\Tools\RedisService;
use Illuminate\Http\Request;

class SpecialUserController extends BaseController
{
    public function add(Request  $request)
    {
        $params = $request->only([
            'user_code','tags','auths'
        ]);
        $v = app('validator')->make($params,
            [
                'user_code' => 'required',
                'tags'  => 'required',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, getValidateMessage($v->messages()));
        }
        $inserted = app('repo_special_user')->insertSpecialUser($params);
        if ($inserted){
            return Response::sendData([],'创建成功');
        }
        throw new BasicException(10001,'创建失败');
    }


    /**
     * 编辑用户特性
     * @param Request $request
     * @return string
     * @throws BasicException
     */
    public function edit(Request $request)
    {
        $userId = $request->input('user_id','');
        $tags = $request->input('tags','');
        $auths = $request->input('auths');
        if (empty($userId) || empty($tags)){
            throw new BasicException(10001,'缺少参数');
        }
        $updateTag = app('repo_special_user')->updateTags($userId,$tags);
        if (!empty($auths)){
            app('repo_special_user')->updateAuths($userId,$auths);
        }
        if ($updateTag){
            return Response::sendData([]);
        }
        throw new BasicException(10001,'更新失败');
    }

    /**
     *
     * @param Request $request
     * @return string
     */
    public function lists(Request $request)
    {
        $userCode = $request->input('user_code','');
        $mobile = $request->input('mobile','');
        $tag = $request->input('tags','');
        $lists = app('repo_special_user')->lists($userCode,$mobile,$tag);
        return Response::sendData($lists);
    }

    /**
     *
     * @return string
     */
    public function stat()
    {
        $stat = app('repo_special_user')->listStat();
        return Response::sendData($stat);
    }


    /**
     * 删除用户及标签
     * @param Request $request
     * @return string
     * @throws BasicException
     */
    public function delete(Request  $request)
    {
        $ids = $request->input('user_ids','');
        if (empty($ids)){
            throw new BasicException(10001,'缺少参数');
        }
        $delete = app('repo_special_user')->delete($ids);
        if ($delete){
            return Response::sendData([]);
        }
        throw new BasicException(10001,'删除失败');
    }


    /**
     *
     * @param Request $request
     * @return string
     * @throws BasicException
     */
    public function userInfo(Request $request)
    {
        $userCode = $request->input('user_code');
        if (empty($userCode)) {
            throw new BasicException(10001, '用户code不能为空');
        }
        $user = app('repo_special_user')->getUserInfo($userCode);
        return Response::sendData($user);
    }
}